import { JsonAsset } from "cc";
import { LocalStorageMgr } from "../Mgr/LocalStorageMgr";

interface IGameData {
    /** 强弹配置 */
    forceConfig: { redStartCount: number; redEndCount: number; forcePopIntervalList: number[]; }[];
    /** 抽奖进度配置配置 */
    drawProcessConfig: { startCount: number; endCount: number; skipCount: number; }[];
    /** 地域配置 */
    confCountry: { /** 国家代码 */ country: string; /** 语言代码 */ language: string; /** 货币符号 */ symbol: string; /**货币格式是否需要去整*/ round: 1 | 0; /**货币格式符号前置*/ front: 1 | 0; /**千位符*/ thousand: 1 | 2 | 3; /**小数点*/ point: 1 | 2 | 3; /** 常用支付渠道 */ channel: string[]; }
    /** 提现配置 */
    withdrawConfig: { cashTotal: number; loginDays: number; level: number; levelWithdrawConfig: { cashAmount: number; tarLevel: number; tarUserLevel: number; }; cashWithdrawConfig: Array<{ cashAmount: number; tarActiveDays: number; tarUserLevel: number; wdProgress: number; withdrawLevel: number; }> }
    /** 解码后的棋盘数据Json */
    boardJson: JsonAsset;
    /** 解码后的word3数据Json */
    word3Json: { ID: string, content: string }[];
    /** 解码后的word4数据Json */
    word4Json: { ID: string, content: string }[];
    /** 解码后的word5数据Json */
    word5Json: { ID: string, content: string }[];
    /** 解码后的word6数据Json */
    word6Json: { ID: string, content: string }[];

    boardData: BoardData;

    gameInitFinish: any;
    
}

export class GameData {
    public static colorHEX = ['#f774ac', '#8ef42a', '#fb3837', '#ffb5ff', '#fb6197', '#b99af2', '#e26edf', '#ff9100', '#f89cfb', '#2bdcf6'];

    private static _data: Partial<IGameData> = {};
    public static data = new Proxy(GameData._data, {
        get(target, prop: keyof IGameData) {
            if (target[prop] === undefined) {
                //@ts-ignore
                target[prop] = LocalStorageMgr.getItem<IUserData[keyof IUserData]>(prop as string) ?? null;
            }
            return target[prop];
        },
        set(target, prop: keyof IGameData, value) {
            //@ts-ignore
            target[prop] = value;
            LocalStorageMgr.setItem(prop as string, value);
            return true;
        }
    }) as IGameData;
}

export class BoardData {
    public type1Data: any[][][] = [];
    public type2Data: any[][][] = [];
    public type3Data: any[][][] = [];
    public type4Data: any[][][] = [];
    public type5Data: any[][][] = [];

    constructor(boardJson: any) {
        this.processBoardData(boardJson);
    }

    private processBoardData(boardJson: any) {
        const types = ['Type_1', 'Type_2', 'Type_3', 'Type_4', 'Type_5'];
        const targetData = [this.type1Data, this.type2Data, this.type3Data, this.type4Data, this.type5Data];

        types.forEach((type, index) => {
            if (boardJson[type] && boardJson[type].data) {
                const data = boardJson[type].data;
                //@ts-ignore
                targetData[index].push(...Object.values(data));
            }
        });
    }
}

